package com.moon.game.command.guest.chat;

import com.moon.game.command.BaseCmd;
import com.moon.game.command.RequestSession;
import com.moon.game.constants.Constants;
import com.moon.game.constants.ErrorCode;
import com.moon.game.exception.GException;
import com.moon.game.manager.PlayerMG;
import com.moon.game.service.ChatService;
import com.moon.game.service.ItemService;
import com.moon.game.vos.ErrorMsg;
import com.moon.game.vos.PlayerVo;

/**
 * @description 发送公聊
 * @author Ranger 作者 E-mail:haozi000005@163.com
 * @version 创建时间：2012-4-28 下午3:03:14
 */
public class SendMessageCmd extends BaseCmd{
	@Override
	protected void runOnce(RequestSession req) throws GException {
		switch (req.getStatus()) {
		case STATU_INIT:
			doInit(req);
			break;
		case STATU_CHECK:
			doCheckSelf(req);
			break;
		case STATU_RETURN:
			doResponse(req);
			break;
		default:
			break;
		}
	}

	@Override
	protected void doResponse(RequestSession req) throws GException {
		String[] ps = req.getClientRequest().getPs();
		if (ps == null || ps.length < 2) {
			doResponsePsError(req);
			return;
		}
		int type = Integer.parseInt(ps[0]);
		if (type > 2 || type < 0) {
			//如果是异常请求类型强制转到公聊
			type = 1;
		}
		String content = ps[1];
		String toPlayerId = "";
		if (ps.length == 3) {
			toPlayerId = ps[2];
		}
		if (content == null || "".equals(content)) {
			doResponseErrorMsg(req, ErrorMsg.build(ErrorCode.ERR_MSG_EMPTY,"发送消息不能为空！"));
			return;
		}
		PlayerVo player = getPlayerVo(req);
		StringBuffer sb = new StringBuffer();
		if (type == Constants.CHAT_WORLD) {
			int ret = ItemService.delItem(player, Constants.TALISMAN_SPEAKER_ID, 1);
			if (ret == Constants.DEL_ITEM_SUCC) {
				//发送世界消息
				Boolean succ = ChatService.sendWorldMessage(player, content);
				if (succ != null && succ) {
					sb.append("发送世界传音成功！");
				}else {
					sb.append("发送世界传音失败，请联系管理员！");
				}
			}else if (ret == Constants.DEL_ITEM_FAIL) {
				sb.append("使用传音符异常！<br/>");
			}else if (ret == Constants.DEL_ITEM_NOT_ENOUGH) {
				sb.append("您的传音符数量不够！<br/>");
			}else if (ret == Constants.DEL_ITEM_LACK) {
				sb.append("缺少传音符，不能进行世界传音！<br/>");
			}
		}else if (type == Constants.CHAT_PRIVATE) {
			//发送私聊消息
			PlayerVo other = PlayerMG.instance.getPlayer(toPlayerId);
			if (other != null) {
				ChatService.sendPrivateMessage(player, toPlayerId, other.getName(), content, true);
			}else {
				ChatService.sendPrivateMessage(player, toPlayerId, "", content, true);
			}
			sb.append("发送私人消息成功！");
		}else {
			int ret = ItemService.delItem(player, Constants.SMALL_HORN_ID, 1);
			if (ret == Constants.DEL_ITEM_SUCC) {
				//发送公聊消息
				Boolean succ = ChatService.sendPublicMessage(player, content);
				if (succ != null && succ) {
					sb.append("发送公聊消息成功！");
				}else {
					sb.append("发送公聊消息失败，请联系管理员！");
				}
			}else if (ret == Constants.DEL_ITEM_FAIL) {
				sb.append("使用小喇叭异常！<br/>");
			}else if (ret == Constants.DEL_ITEM_NOT_ENOUGH) {
				sb.append("您的小喇叭数量不够！<br/>");
			}else if (ret == Constants.DEL_ITEM_LACK) {
				sb.append("缺少小喇叭，不能进行公聊喊话！<br/>");
			}
		}
		req.setVO(TAG_INFO, sb.toString());
		req.setVO("cur_type", type);
		response(req, req.getClientResponse());
	}
}
